//
//  WebviewControl.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "WebviewControl.h"
#import <DKNightVersion.h>
#import "webOfdic.h"
@interface WebviewControl ()<UIWebViewDelegate>

@property (nonatomic,retain)UIActivityIndicatorView *biao;

@property (nonatomic,retain)NSMutableArray *array;

@end

@implementation WebviewControl

-(void)dealloc{
    [_array release];
    [_titlea release];
    [_desc release];
    [_video_url release];
    [_published release];
    [_weight_new release];
    [_idd release];
    [_pic_url release];
    
    [_urlstring release];
    [_biao release];
    [_webview release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"xin2"] style:UIBarButtonItemStylePlain target:self action:@selector(action)];
    
    [self changgecolorwithCount:self.count];
    
    
    self.webview = [[UIWebView alloc]init];
    
    
    
    self.webview.frame = CGRectMake(0, 0, 414, 735);
    [self.view addSubview:self.webview];
    self.webview.backgroundColor = [UIColor whiteColor];
    
    self.webview.delegate = self;
    
    
    

    NSURL* url = [NSURL URLWithString:self.urlstring];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
    
    [self.webview loadRequest:request];
    [self.webview release];

    self.view.backgroundColor = [UIColor orangeColor];
    
  }


-(void)changgecolorwithCount:(NSInteger)count{
    
    
    if (count == 1) {
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor yellowColor];
        
    }
    if (count == 0) {
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    }
}


-(void)action{
    //空心变实心
    NSLog(@"收藏");
    
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
    
    NSMutableArray *arr =  [NSMutableArray arrayWithContentsOfFile:file];
    
    NSLog(@"%ld",arr.count);
    //写入数组数据的初始化
    self.array = [NSMutableArray array];
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor yellowColor];
    
    if (arr.count == 0) {
        webOfdic *dic = @{@"title":self.titlea,@"desc":self.desc,@"video_url":self.video_url,@"published":self.published,@"weight_new":self.weight_new,@"idd":self.idd,@"pic_url":self.pic_url}.mutableCopy;
        
        [self.array addObject:dic];
        
        NSLog(@"asdasdad%@",[self.array objectAtIndex:0]);
        
        NSString  *filejin = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
        
        [self.array writeToFile:filejin atomically:YES];
        
        NSLog(@"%@",NSHomeDirectory());
    }else{
        int count = 0;
        for (webOfdic *dic in arr) {
            
            if ([[dic objectForKey:@"title"] isEqualToString:self.titlea]) {
                
                count ++;
                NSLog(@"%d",count);
            }else{
                
                count += 0;
                
                NSLog(@"%d",count);
            }
        }
        
        
        
        if (count == 0) {
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor yellowColor];
            
            
            webOfdic *dic = @{@"title":self.titlea,@"desc":self.desc,@"video_url":self.video_url,@"published":self.published,@"weight_new":self.weight_new,@"idd":self.idd,@"pic_url":self.pic_url}.mutableCopy;
            
            
            NSLog(@"%@",dic);
            
            [arr addObject:dic];
            
            NSString  *filejin = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
            
            [arr writeToFile:filejin atomically:YES];
            
            NSLog(@"%@",NSHomeDirectory());
        }else if (count == 1){
            
            
            webOfdic *dic = @{@"title":self.titlea,@"desc":self.desc,@"video_url":self.video_url,@"published":self.published,@"weight_new":self.weight_new,@"idd":self.idd,@"pic_url":self.pic_url}.mutableCopy;
            
            NSLog(@"%@",dic);
            
           // 移除从复的
            [arr removeObject:dic];
            
            
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor  whiteColor];
            
            NSString  *filejin = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
            
            [arr writeToFile:filejin atomically:YES];
            
            NSLog(@"%@",NSHomeDirectory());
            
        }
        
    }
    
}



-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSLog(@"webDidstarLoad");
    

    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,width ,height)];
    
    [view setTag:100];
    
    
    view.backgroundColor = [UIColor blackColor];
    
    view.alpha = 0.4;
    
    [self.webview addSubview:view];
    
    
    view.center = self.webview.center;
    self.biao = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    
    self.biao.center = self.view.center;
    
    [self.biao setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [view addSubview:self.biao];
    
    [self.biao startAnimating];
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
   

    
    [self.biao startAnimating];
    
    UIView *view = [self.view viewWithTag:100];
    
    [view removeFromSuperview];
    
     self.navigationItem.title = [NSString stringWithString:[webView stringByEvaluatingJavaScriptFromString:@"documnet.tilte"]];
//     self.navigationItem.title = [NSString stringWithString:@"asdads"];
    
//           [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('news')[0].style.background='#2C2C2C'"];
//        
    
    
    
    
    NSLog(@"webDidtstarFinish");
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [self.biao startAnimating];
    UIView *view = [self.view viewWithTag:100];
    [view removeFromSuperview];
    NSLog(@"didFailWithError:%@",error);
    
   
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
